<?php
/**
 * Zend Framework Contribution
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Mwb
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    1.0 Indice.php 3 mai 2012 Sergio DE VASCONCELOS
 */

require_once('Zend/Mwb/Dom/Abstract.php');

/**
 * MySQL Workbench document
 *
 * @category   Zend
 * @package    Zend_Mwb
 */
class Zend_Mwb_Dom_Indice extends Zend_Mwb_Dom_Abstract
{
	protected $name;
	protected $type;
	
	/**
	 * @var Zend_Mwb_Dom_Columns
	 */
	protected $indexColumns;
	
	public function getName() {
		if (is_null($this->name)) {
			$this->name = $this->loadName();
		}
		return $this->name;
	}
	
	public function setName($name) { }
	public function getType() {
		if (is_null($this->type)) {
			$this->type = $this->loadType();
		}
		return $this->type;
	}
	public function setType($type) { }
	
	public function getIndexColumns() {
		if (is_null($this->indexColumns)) {
			$this->indexColumns = $this->loadIndexColumns();
		}
		return $this->indexColumns;
	}
	public function setIndexColumns($name) { }
	//public function addCollumn($column) { }

	public function parse() {
		$this->xpath = new DOMXPath($this->node->ownerDocument);
		
		$this->name        = $this->loadName();
		$this->type        = $this->loadType();
		//$this->columns     = $this->loadIndexColumns();
	}
	/**
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function loadName() {
    	$element = $this->xpath->query("value[@key='name']", $this->node)->item(0);
    	if (isset($element)) {
        	return $element->textContent;
        } else {
        	require_once 'Zend/Mwb/Exception.php';
        	throw new Zend_Mwb_Exception('Dom_Exception_Null($this->node)');
        }
	}
	/**
	 * @param String $name
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function saveName($name, DOMElement $dom=null) {
    	$element = $this->xpath->query("value[@key='name']", $this->node)->item(0);
        if (isset($element)) {
        	$element->textContent = $name;
        }// FIXME else create/lazyload
	}
	/**
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function loadType() {
    	$element = $this->xpath->query("value[@key='indexType']", $this->node)->item(0);
    	if (isset($element)) {
        	return $element->textContent;
        } else {
        	require_once 'Zend/Mwb/Exception.php';
        	throw new Zend_Mwb_Exception('Dom_Exception_Null($this->node)');
        }
	}
	
	/**
	 * @param DOMElement $dom
	 * @return Zend_Mwb_Dom_Columns
	 */
	protected function loadIndexColumns() {
    	$elements = $this->xpath->query("value[@key='columns']/value", $this->node);
    	return new Zend_Mwb_Dom_IndexColumns($elements, $this);
	}

	public function create() {
		
	}
}
